import 'package:flutter/material.dart';

class MListBuilder extends StatefulWidget {
  @override
  _MListBuilderState createState() => _MListBuilderState();
}

class _MListBuilderState extends State<MListBuilder> {
  var items = List<String>();
  ScrollController controller;

  void getData() {
    for (int i = 0; i < 50; i++) {
      items.add("假数据:${i}");
    }
  }

  @override
  void initState() {
    super.initState();
    controller = ScrollController()
      ..addListener(() {
        print('===> ${controller.position}');
      });
    this.getData();
  }

  Widget _itemView(String content, int position) {
    return Container(
      height: 50,
      child: InkWell(
        onTap: () {
          print("点击了${content}");
        },
        child: Center(
          child: Text(
            content,
            style: TextStyle(
                color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
      ),
      color: position % 2 == 0 ? Colors.deepOrange : Colors.amberAccent,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("ListView.Builder"),
      ),
      body: ListView.builder(
          controller: controller,
          itemCount: items.length,
//          physics: NeverScrollableScrollPhysics(),//不可滚动
          itemBuilder: (BuildContext context, int index) {
            return this._itemView(items[index], index);
          }),
    );
  }
}
